package com.er.mq;

/**
 * Created by shixj on 2017/9/18.
 */


import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class JMSConfig {

    public static  final  String DESTINATION="sample.mq";

    /**
     * 点对点模式
     * @return
     */
    @Bean
    public Queue queue() {
        return new ActiveMQQueue(DESTINATION);
    }
    // queue模式的ListenerContainer
    @Bean("queueContainerFactory")
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
    /**
     * topic模式
     */
    @Bean
    public Topic topic() {
        return new ActiveMQTopic(DESTINATION);
    }

    @Bean("topicContainerFactory")
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

}